package com.xiaoma.juc.countdownlatch;

import java.util.concurrent.CountDownLatch;

/**
 * @author xiaomacdut
 * @date 2020年2月7日 上午11:52:05
 */
public class CountDownLatchTest{
    
    public static void main(String [] args){
        final CountDownLatch latch = new CountDownLatch(2);
        System.out.println("主线程开始执行…… ……");

        new Thread(new Runnable() {

            @Override
            public void run(){
                try{
                    Thread.sleep(3000);
                    System.out.println("子线程：" + Thread.currentThread().getName() + "执行");
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
                latch.countDown();//countDown后可以继续干自己的事情
                System.out.println("执行 countdown");
            }

        }).start();

        new Thread(new Runnable() {

            @Override
            public void run(){
                try{
                    Thread.sleep(3000);
                    System.out.println("子线程：" + Thread.currentThread().getName() + "执行");
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
                latch.countDown();
                System.out.println("执行 countdown");
            }

        }).start();

        System.out.println("等待两个线程执行完毕…… ……");
        try{
            latch.await();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("两个子线程都执行完毕，继续执行主线程");
    }
}
